import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  HttpStatus,
  HttpCode,
  UseInterceptors,
  UploadedFile,
  BadRequestException,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { CustomerQuotationReplyService } from './customer-quotation-reply.service';
import { CreateCustomerQuotationReplyDto } from './dto/create-customer-quotation-reply.dto';
import { UpdateCustomerQuotationReplyDto } from './dto/update-customer-quotation-reply.dto';
import { OssUtilService } from '../common/upload/oss-util.service';
import { UploadPicturesService } from '../common/upload/upload-pictures.service';
import { v4 as uuidv4 } from 'uuid';

@Controller('customer-quotation-reply')
export class CustomerQuotationReplyController {
  constructor(
    private readonly customerQuotationReplyService: CustomerQuotationReplyService,
    private readonly ossUtilService: OssUtilService,
    private readonly uploadPicturesService: UploadPicturesService,
  ) {}





  // 上传客户报价回复图片接口
  @Post('uploadimg')
  @UseInterceptors(FileInterceptor('file'))
  async uploadImage(@UploadedFile() file: any) {
    if (!file) {
      throw new BadRequestException('请选择要上传的文件');
    }

    const fileName = file.originalname;
    const suffixName = fileName.substring(fileName.lastIndexOf('.'));
    const newFileName = uuidv4() + suffixName;

    try {
      const result = await this.ossUtilService.uploadFile(
        'hasmarket',
        'customerquotereply',
        newFileName,
        file.buffer,
      );

      return {
        success: true,
        data: {
          url: result.url,
          name: result.name,
          originalName: fileName,
          size: file.size,
          mimeType: file.mimetype,
        },
        message: '上传成功',
      };
    } catch (error) {
      console.error('OSS上传错误:', error);
      throw new BadRequestException(`上传失败: ${error.message}`);
    }
  }

  // 添加图片记录到数据库
  @Post('addimageitem')
  async addImageItem(@Body() imageData: any) {
    try {
      const result = await this.uploadPicturesService.create({
        serviceId: imageData.serviceId,
        name: imageData.name,
        imageUrl: imageData.imageUrl,
        suffix: imageData.suffix || '.jpg',
      });

      return {
        success: true,
        data: result,
        message: '图片记录添加成功',
      };
    } catch (error) {
      throw new BadRequestException('图片记录添加失败: ' + error.message);
    }
  }

  @Get('quote/:quoteid')
  async findByQuoteid(@Param('quoteid') quoteid: string) {
    const result = await this.customerQuotationReplyService.findByQuoteid(quoteid);
    return {
      success: true,
      data: result,
      message: '获取客户报价回复列表成功'
    };
  }





  // 测试端点：验证图片查询
  @Get('test-image/:serviceId')
  async testImageQuery(@Param('serviceId') serviceId: string) {
    const result = await this.customerQuotationReplyService.testFindImageByServiceId(serviceId);
    return {
      success: true,
      data: result,
      message: '图片查询测试完成'
    };
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    const result = await this.customerQuotationReplyService.findById(+id);
    return {
      success: true,
      data: result,
      message: '获取客户报价回复详情成功'
    };
  }




} 